package com.ruike.alisurface.ui.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.bean.SlotBean;


import java.util.List;

/**
 * 货道检测适配器
 * Created by czb on 2019-12-02.
 */

public class SlottestAdapter extends RecyclerView.Adapter<SlottestAdapter.ViewHolder> {

    Context context;
    List<SlotBean> datas;

    public SlottestAdapter(Context context, List<SlotBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void setData(List<SlotBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_solt_index, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.index_text.setText(String.valueOf(datas.get(i).getIndex()));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onGoodsSlotClick(i);
            }
        });
        SlotBean goodsBean = datas.get(i);
        if (goodsBean.getTurnNumber() > 0) {
//            L.iTag("测试", "adapter货道转动成功");
            viewHolder.indexLlayout.setBackgroundResource(R.drawable.shape_round_right_bg);
            viewHolder.index_text.setTextColor(Color.parseColor("#00B000"));
        } else if (goodsBean.getTurnNumber() < 0) {
//            L.iTag("测试", "adapter货道转动失败");
            viewHolder.indexLlayout.setBackgroundResource(R.drawable.shape_round_error_bg);
            viewHolder.index_text.setTextColor(Color.parseColor("#DE267A"));
        } else {
//            L.iTag("测试", "adapter货道转动未转动");
            viewHolder.indexLlayout.setBackgroundResource(R.drawable.shape_round_bg);
            viewHolder.index_text.setTextColor(Color.parseColor("#108DE7"));
        }
    }

    public OnItemClickListenerr onItemClickListener;

    public void setOnItemClickListenerr(OnItemClickListenerr onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 删除购物车商品
     */
    public interface OnItemClickListenerr {
        void onGoodsSlotClick(int index);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout indexLlayout;
        TextView index_text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            indexLlayout = itemView.findViewById(R.id.index_llayout);
            index_text = itemView.findViewById(R.id.index_text);
        }
    }
}
